package net.dotool;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * @author 李岩飞
 * @email eliyanfei@126.com
 * @date 2018年3月23日下午5:50:34
 */
public class FetchTools {
	static long time = 0;

	public static void main(String[] args) {
		fetch("2018-03-26 14:56", "E:\\works\\simpleframe\\WebRoot", "f:\\demo");
	}

	public static void initTime(final String value) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			time = sdf.parse(value).getTime();
		} catch (final ParseException e) {
		}
	}

	/**
	 * 根据文件修改时间拉取文件
	 * 
	 * @param time
	 * @param path
	 * @param dist
	 */
	private static void fetch(String time, String path, String dist) {
		initTime(time);
		File file = new File(path);
		for (File f : file.listFiles()) {
			listFile(f, path, dist);
		}
	}

	private static void listFile(File file, String path, String dist) {
		if (file.isFile()) {
			String filePath = file.getAbsolutePath().replace(path, dist);
			long t = file.lastModified();
			if (t >= time) {
				File newFile = new File(filePath);
				newFile.getParentFile().mkdirs();
				try {
					FileTools.copyFile(file.getAbsolutePath(), newFile.getAbsolutePath());
				} catch (Exception e) {
					e.printStackTrace();
				}
				System.out.println(filePath);
			}
		} else {
			for (File f : file.listFiles()) {
				listFile(f, path, dist);
			}
		}
	}

}
